package com.maas.util.mybatis.jpa.littlenb.conditions.plus.extension.incrementer;

import com.littlenb.mybatisjpa.keygen.IdGenerator;
import com.maas.util.mybatis.jpa.littlenb.conditions.plus.annotation.DbType;
import com.maas.util.mybatis.jpa.littlenb.conditions.plus.core.incrementer.IKeyGenerator;
import com.maas.util.mybatis.jpa.littlenb.conditions.plus.core.toolkit.IdWorker;
import com.twitter.snowflake.support.SecondsIdGeneratorFactory;

public class MysqlKeyGenerator implements IKeyGenerator {
	private com.twitter.snowflake.sequence.IdGenerator idGenerator;



	public  void createIdGenerator() {
		SecondsIdGeneratorFactory idGeneratorFactory = new SecondsIdGeneratorFactory(1483200000L);
		//workerId
		idGenerator = idGeneratorFactory.create(IdWorker.getId());
	}

	public  void initIdGenerator(){
		if (idGenerator == null) {
			createIdGenerator();
		}
	}

	@Override
	public String executeSql(String incrementerName) {
		initIdGenerator();
		//MyIdGenerator idGenerator =MyIdGenerator.getInstance();
		//Integer id = MyLangTools.trfInteger(idGenerator.nextId().toString());

		//log.info("mybatis plus keyGenerator: " + incrementerName + "(" + workerId + "," + dataCenterId + ")");
		long uid = idGenerator.nextId();
		return "select " + uid + " from dual";
	}

	@Override
	public DbType dbType() {
		return DbType.MYSQL;
	}
}